<?php
    if(isset($_POST['type'])){
        $type=$_POST['type'];
        switch($type){
            case 'modify_box_show':
                @$code=$_POST['code'];
                modify_box_show($code);
                break;
            case 'modify_module':
                $code=$_POST['code'];
                $title=$_POST['title'];
                $semester=$_POST['Semster'];
                $lecture=$_POST['lecture'];
                $tutorial=$_POST['tutorial'];
                $lab=$_POST['lab'];
                $credit=$_POST['credit'];
                $studentnum=$_POST['studentnum'];
                modify_module($code,$title,$semester,$lecture,$tutorial,$lab,$credit,$studentnum);
                modify_reshow($code);
                break;
            case 'insert_module':
                $code=$_POST['code'];
                $title=$_POST['title'];
                $semester=$_POST['Semster'];
                $lecture=$_POST['lecture'];
                $tutorial=$_POST['tutorial'];
                $lab=$_POST['lab'];
                $credit=$_POST['credit'];
                $studentnum=$_POST['studentnum'];
                insert_module($code,$title,$semester,$lecture,$tutorial,$lab,$credit,$studentnum);
                break;
            case 'delete_box_show':
                @$code=$_POST['code'];
                delete_box_show($code);
                break;
            case 'delete_module':
                $code=$_POST['code'];
                delete_module($code);
                break;
            default:
             echo "error:1024";
        }
    }
    function delete_module($code){
        include"sqlconnect.php";
        $sql="DELETE FROM module WHERE moduleCode='$code'";
        mysqli_query($conn,$sql);
        mysqli_close($conn);
    }
    function delete_box_show($code){
        if($code==''){
        echo"<div class='modal-dialog'>
          <div class='modal-content'>
             <div class='modal-header'>
                <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                <h4 class='modal-title' id='myModalLabel'>Please click the module firstly!</h4>
             </div>
          </div>
       </div>";
         return ;
        }
        else{
            echo"
            <div class='modal-dialog'>
                <div class='modal-content'>
                    <div class='modal-header'>
                        <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                        <h4 class='modal-title' id='myModalLabel'>$code information modify</h4>
                    </div>";
            include"sqlconnect.php";
            $sql="SELECT count(1) as num FROM task WHERE moduleCode='$code'";
            $result=mysqli_query($conn,$sql);
            while($row=mysqli_fetch_array($result)){
                $num=$row['num'];
            }
            if($num>0){
                echo"
                    <div class='modal-body'>
                        <h4>$code is already used!It can't be deleted!</h4>
                    </div>   
                    <div class='modal-footer'>                   
                        <button type='button' class='btn btn-default' data-dismiss='modal'>CLOSE</button>
                        </div>    
                    </div>
                </div>";
            }else{
                echo"
                    <div class='modal-body'>
                        <h4>Are you sure you want to delete $code</h4>
                        <h5 class='text-danger'>The deleted data can't be recovered!!!</h5>
                    </div>   
                    <div class='modal-footer'>                   
                        <button id='delete_module_btn' type='button' class='btn btn-danger' data-id='$code' data-dismiss='modal'>DELETE</button>
                        </div>    
                    </div>
                </div>";
            }
            
        }
    }
    function insert_module($code,$title,$semester,$lecture,$tutorial,$lab,$credit,$studentnum){
        if($lecture==''){
            $lecture=0;
        }
        if($tutorial==''){
            $tutorial=0;
        }
        if($lab==''){
            $lab=0;
        }
        if($credit==''){
            $credit=0;
        }
        if($studentnum==''){
            $studentnum=0;
        }
        include 'sqlconnect.php';
        $sql="SELECT *FROM module WHERE moduleCode='$code'";
        if(mysqli_fetch_array(mysqli_query($conn,$sql))){
            echo"<div class='fail-modify'>The $code has repeated</div>";
        }
        else{
            $sql="INSERT INTO module 
            (moduleCode, moduleTitle, semester,lecture,tutorial,lab,credit,studentNumber) 
            VALUES ('$code', '$title', '$semester', $lecture, $tutorial, $lab, $credit,$studentnum)";
            mysqli_query($conn,$sql);
            echo"<div class='success-modify'>$code insert successful!</div>";
        }
        mysqli_close($conn);
    }
    function modify_reshow($code){
        include 'sqlconnect.php';
        $sql="SELECT *FROM module WHERE moduleCode='$code'";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $code=$row['moduleCode'];
            $title=$row['moduleTitle'];
            $semester=$row['semester'];
            $lecture=$row['lecture'];
            $tutorial=$row['tutorial'];
            $lab=$row['lab'];
            $credit=$row['credit'];
            $studentnum=$row['studentNumber'];
            echo"
                    
                    <td>$code</td>
                    <td>$title</td>
                    <td>$semester</td>
                    <td>$lecture</td>
                    <td>$tutorial</td>
                    <td>$lab</td>
                    <td>$credit</td>
                    <td>$studentnum</td>
                    <td><span class='success-coin glyphicon glyphicon-ok'></span></td>
                ";
        }
        mysqli_close($conn);
    }
    function modify_module($code,$title,$semester,$lecture,$tutorial,$lab,$credit,$studentnum){
        if($lecture==''){
            $lecture=0;
        }
        if($tutorial==''){
            $tutorial=0;
        }
        if($lab==''){
            $lab=0;
        }
        if($credit==''){
            $credit=0;
        }
        if($studentnum==''){
            $studentnum=0;
        }
        include 'sqlconnect.php';
        $sql="UPDATE module SET 
        moduleTitle='$title',semester='$semester',lecture=$lecture,
        tutorial=$tutorial,lab=$lab,credit=$credit,studentNumber=$studentnum WHERE moduleCode='$code'";
        mysqli_query($conn,$sql); 
    }
    function modify_box_show($code){
        if($code==''){
        echo"<div class='modal-dialog'>
          <div class='modal-content'>
             <div class='modal-header'>
                <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                <h4 class='modal-title' id='myModalLabel'>Please click the module firstly!</h4>
             </div>
          </div>
       </div>";
         return ;
        }
        include 'sqlconnect.php';
        $sql="SELECT *FROM module WHERE moduleCode='$code'";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $title=$row['moduleTitle'];
            $semester=$row['semester'];
            $lecture=$row['lecture'];
            $tutorial=$row['tutorial'];
            $lab=$row['lab'];
            $credit=$row['credit'];
            $studentnum=$row['studentNumber'];
        }
        echo"<div class='modal-dialog'>
          <div class='modal-content'>
             <div class='modal-header'>
                <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                <h4 class='modal-title' id='myModalLabel'>$code information modify</h4>
             </div>
             <div class='modal-body'>
                <form class='form-horizontal' role='form'>
                    <div class='form-group'>
                        <label class='col-sm-3 home-label control-label'>ModuleTitle:</label>
                        <div class='col-sm-9'>
                            <input type='text' class='form-control' id='title' value='$title'>
                        </div>
                    </div>
                     <div class='form-group'>
                        <label class='col-sm-3 home-label control-label'>Semster:</label>
                        <div class='col-sm-9'>
                            <select class='form-control' id='Semster'>";
                            if($semester=='First Semester'){
                                echo"<option value='First Semester' selected='selected'>First Semester</option>
                                     <option value='Second Semester'>Second Semester</option>
                                     <option value='Whole Session'>Whole Session</option>";
                            }
                            else if($semester=='Second Semester'){
                                echo"<option value='First Semester'>First Semester</option>
                                     <option value='Second Semester' selected='selected'>Second Semester</option>
                                     <option value='Whole Session'>Whole Session</option>";
                            }
                            else{
                                 echo"<option value='First Semester'>First Semester</option>
                                     <option value='Second Semester' >Second Semester</option>
                                     <option value='Whole Session' selected='selected'>Whole Session</option>";
                            }
                        echo"</select>
                        </div>
                    </div>
                    <div class='form-group'>
                        <label class='col-sm-3 home-label control-label'>Lecture:</label>
                        <div class='col-sm-9'>
                            <input type='text' class='form-control' id='lecture' value='$lecture'>
                        </div>
                    </div>
                   
                    <div class='form-group'>
                        <label class='col-sm-3 home-label control-label'>Tutorial:</label>
                        <div class='col-sm-9'>
                            <input type='number' class='form-control' id='tutorial' value='$tutorial'>
                        </div>
                    </div>
                    <div class='form-group'>
                        <label class='col-sm-3 home-label control-label'>Lab Num:</label>
                        <div class='col-sm-9'>
                            <input type='number' class='form-control' id='lab' value='$lab'>
                        </div>
                    </div>
                    <div class='form-group'>
                        <label class='col-sm-3 home-label control-label'>Credit:</label>
                        <div class='col-sm-9'>
                            <input type='number' class='form-control' id='credit' value='$credit'>
                        </div>
                    </div>
                    <div class='form-group'>
                        <label class='col-sm-3 home-label control-label'>Studentnum:</label>
                        <div class='col-sm-9'>
                            <input type='number' class='form-control' id='studentnum' value='$studentnum'>
                        </div>
                    </div>
                </form>
            </div>   
            <div class='modal-footer'>                   
                <button id='modify_module_btn' type='button' class='btn btn-primary' data-id='$code' data-dismiss='modal'>MODIFY</button>
                <button type='button' class='btn btn-default' data-dismiss='modal'>CLOSE</button>
            </div>    
         </div>
        </div>";
        mysqli_close($conn);
    }
    function show_title(){
        echo"<table class='table table-bordered'>
            <thead>
                <tr>
                    <th>Code</th>
                    <th>Title</th>
                    <th>Semester</th>
                    <th>Lecture</th>
                    <th>Tutorial</th>
                    <th>Lab</th>
                    <th>Credit Value</th>
                    <th>Number of Student</th>
                </tr>    
            </thead>";
    }
    function show_all(){
        show_title();
        echo"<tbody>";
        include 'sqlconnect.php';
        $sql="SELECT *FROM module";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $code=$row['moduleCode'];
            $title=$row['moduleTitle'];
            $semester=$row['semester'];
            $lecture=$row['lecture'];
            $tutorial=$row['tutorial'];
            $lab=$row['lab'];
            $credit=$row['credit'];
            $studentnum=$row['studentNumber'];
            echo"
                    <tr id='$code' class='module' data-id='$code'>
                    <td>$code</td>
                    <td>$title</td>
                    <td>$semester</td>
                    <td>$lecture</td>
                    <td>$tutorial</td>
                    <td>$lab</td>
                    <td>$credit</td>
                    <td>$studentnum</td>
                    </tr>
                ";
        }
        echo"</tbody>
        </table>";
        mysqli_close($conn);
    }
?>